package com.common.widght;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.common.widght.smarttablayout.SmartTabLayout;
import com.common.widght.smarttablayout.support.FragmentPagerItem;
import com.common.widght.smarttablayout.support.FragmentPagerItemAdapter;
import com.common.widght.smarttablayout.support.FragmentPagerItems;
import com.common.widght.smarttablayout.support.FragmentPagerViewAdapter;
import com.lsz.deport.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TabViewPagerLayout extends LinearLayout {

    private FragmentPagerItemAdapter adapter;
    private FragmentPagerItems pages;
    private ViewPager viewPager;
    private SmartTabLayout viewPagerTab;
    private Context context;

    public TabViewPagerLayout(Context context) {
        super(context);
        this.context = context;
    }

    public TabViewPagerLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public TabViewPagerLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        View layout = LayoutInflater.from(context).inflate(R.layout.layout_tab_fragment, this, true);

        viewPagerTab = layout.findViewById(R.id.viewpagertab);
        viewPager = layout.findViewById(R.id.viewpager);
    }

    public void setTabTextColor(int tabTextColor){
        viewPagerTab.setDefaultTabTextColor(tabTextColor);
    }

    public void setFragmentPagerItem(List<TabViewBean> tabViewList) {
        List<Fragment> fragmentList = new ArrayList<>();
        List<String> titleList = new ArrayList<>();
        for (TabViewBean tabViewBean : tabViewList) {
            fragmentList.add(tabViewBean.tabValue);
            titleList.add(tabViewBean.tabKey);
        }
        FragmentPagerViewAdapter pagerAdapter = new FragmentPagerViewAdapter(((FragmentActivity) context).getSupportFragmentManager());
        pagerAdapter.setFragmentPager(fragmentList, titleList);
        viewPager.setAdapter(pagerAdapter);
        viewPagerTab.setViewPager(viewPager);
    }

    public void setPagerListener(ViewPager.OnPageChangeListener onPageChangeListener){
        viewPagerTab.setOnPageChangeListener(onPageChangeListener);
    }

    public void setPagerItemListener(PagerItemListener pagerItemListener){
        if(pages != null){
            pagerItemListener.onFragmentPagerItems(pages);
        }
    }

    public interface PagerItemListener{

        void onFragmentPagerItems(FragmentPagerItems fragmentPagerItems);

    }
}
